package com.example.bbs.conf;

import com.example.bbs.handlerinterceptor.AdminHandlerInterceptor;
import com.example.bbs.handlerinterceptor.UserHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Component
/**
 * 静态工作目录和拦截器部分配置
 */
public class StaticFileConfBean implements WebMvcConfigurer {

    @Autowired
    private FileConf fileConf;
    @Autowired
    private AdminHandlerInterceptor adminHandlerInterceptor;
    @Autowired
    private UserHandlerInterceptor userHandlerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excet = new ArrayList<>();
        excet.add("/api/admin/login");
        excet.add("/api/admin/statue");
        registry.addInterceptor(adminHandlerInterceptor).addPathPatterns("/api/admin/**").excludePathPatterns(excet);
        List<String> excet2 = new ArrayList<>();
        excet2.add("/api/user/login");
        excet2.add("/api/user/register");
        excet2.add("/api/user/statue");
        excet2.add("/api/user/info");
        registry.addInterceptor(userHandlerInterceptor).addPathPatterns("/api/user/**").excludePathPatterns(excet2);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        // 解决 SWAGGER 404报错
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/file/**")
                .addResourceLocations("file:" + fileConf.getFileSave());
    }

}
